package com.travelsky.airchina.boarding.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.travelsky.airchina.boarding.dao.NewsColumnDAO;
import com.travelsky.airchina.boarding.domain.NewsColumn;
import com.travelsky.airchina.boarding.dto.NewsColumnView;

/**
 *
 * @author haichenwang
 *
 */
@Transactional
@Service("newsColumnService")
public class NewsColumnService{
	@Autowired
	private NewsColumnDAO newsColumnDAO;
	
	public List<NewsColumn> findNewsColumnsByParentId(Long id) {
		return newsColumnDAO.findNewsColumnsByParentId(id);
	}
	@Transactional(readOnly=true)
	public NewsColumn getNewsColumn(Long id) {
		return newsColumnDAO.findById(id);
	}
	public void saveNewsColumn(NewsColumn newsColumn) {
		newsColumnDAO.makePersistent(newsColumn);
		if(null!=newsColumn.getParentColumn() && newsColumn.getParentColumn().getId()!=null){
			newsColumnDAO.updateNewsColumnLeaf(newsColumn.getParentColumn().getId(), "0");
		}
	}
	public void updateNewsColumn(NewsColumn newsColumn,Long oldParentNewsColumnId) {
		this.saveNewsColumn(newsColumn);
		if(oldParentNewsColumnId!=null){
			newsColumnDAO.updateNewsColumnLeaf(oldParentNewsColumnId);
		}
	}
	@Transactional(readOnly=true)
	public boolean isNewsColumnCodeExist(String code) {
		return newsColumnDAO.isNewsColumnCodeExist(code);
	}
	@Transactional(readOnly=true)
	public boolean isNewsColumnNameExist(String name) {
		return newsColumnDAO.isNewsColumnNameExist(name);
	}
	@Transactional(readOnly=true)
	public boolean isNewsColumnNameUnique(String name) {
		return newsColumnDAO.isNewsColumnNameUnique(name);
	}
	@Transactional(readOnly=true)
	public List<NewsColumn> findAllParentNewsColumnsByChildId(Long id) {
		return newsColumnDAO.findAllParentNewsColumnsByChildId(id);
	}
	@Transactional(readOnly=true)
	public int getNewsColumnSum() {
		return newsColumnDAO.getNewsColumnSum();
	}
	@Transactional(readOnly=true)
	public Map<Long, List<NewsColumn>> findChildrenNewsColumnByParentId(
			List<Long> newsColumnIds, Long id) {
		List<NewsColumn> list = newsColumnDAO.findNewsColumnsByParentId(newsColumnIds);
		Map<Long,List<NewsColumn>> map = new LinkedHashMap<Long,List<NewsColumn>>();
		if(list!=null && list.size()>0){
			for(NewsColumn temp:list){
				if(temp.getParentColumn().getId()!=null && !temp.getId().equals(id)){
					if(!map.containsKey(temp.getParentColumn().getId())){
						List<NewsColumn> listTemp = new ArrayList<NewsColumn>();
						listTemp.add(temp);
						map.put(temp.getParentColumn().getId(), listTemp);
					}else{
						map.get(temp.getParentColumn().getId()).add(temp);
					}
				}
			}
		}
		return map;
	}
	@Transactional(readOnly=true)
	public Map<Long, List<NewsColumn>> findChildrenNewsColumnByParentId(
			List<Long> newsColumnIds) {
		List<NewsColumn> list = newsColumnDAO.findNewsColumnsByParentId(newsColumnIds);
		Map<Long,List<NewsColumn>> map = new LinkedHashMap<Long,List<NewsColumn>>();
		if(list!=null && list.size()>0){
			for(NewsColumn temp:list){
				if(temp.getParentColumn().getId()!=null){
					if(!map.containsKey(temp.getParentColumn().getId())){
						List<NewsColumn> listTemp = new ArrayList<NewsColumn>();
						listTemp.add(temp);
						map.put(temp.getParentColumn().getId(), listTemp);
					}else{
						map.get(temp.getParentColumn().getId()).add(temp);
					}
				}
			}
		}
		return map;
	}
	public boolean deleteNewsColumn(Long id) throws DataAccessException {
		try{
			List<Long> childNewsColumnIds = new ArrayList<Long>();
			childNewsColumnIds.add(id);
			newsColumnDAO.findAllColumnListByParentColumnId(id,childNewsColumnIds);
			if(childNewsColumnIds.size()>1){
				Collections.reverse(childNewsColumnIds);
			}
			newsColumnDAO.deleteNewsByColumnIds(childNewsColumnIds);
			newsColumnDAO.deleteVideoByColumnIds(childNewsColumnIds);
			newsColumnDAO.deleteRoleColumnByColumnIds(childNewsColumnIds);
			for(Long temp:childNewsColumnIds){
				newsColumnDAO.makeTransient(newsColumnDAO.findById(temp));
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public List<NewsColumn> findAllNewsColumns() {
		return newsColumnDAO.findAll();
	}
	public List<NewsColumn> findAuthorityNewsColumnsByParentId(Long id,
			List<Long> roleColumnIdList) {
		List<NewsColumn> list = new ArrayList<NewsColumn>();
		if(roleColumnIdList!=null && roleColumnIdList.size()>0){
			List<NewsColumn> newsColumns = newsColumnDAO.findNewsColumnsByParentId(id);
			if(newsColumns!=null&&newsColumns.size()>0){
				for(NewsColumn temp:newsColumns){
					if(roleColumnIdList.contains(temp.getId())){
						list.add(temp);
					}
				}
			}
		}
		return list;
	}
	public List<NewsColumnView> findAuthorityNewsColumnViewsByParentId(Long id,
			List<Long> roleColumnIdList) {
		List<NewsColumnView> list = new ArrayList<NewsColumnView>();
		if(roleColumnIdList!=null && roleColumnIdList.size()>0){
			List<NewsColumnView> newsColumns = newsColumnDAO.findNewsColumnViewsByParentId(id);
			if(newsColumns!=null&&newsColumns.size()>0){
				for(NewsColumnView temp:newsColumns){
					if(roleColumnIdList.contains(temp.getId())){
						list.add(temp);
					}
				}
			}
		}
		return list;
	}

}
